﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MLY.Reports.DataReports
{
    public partial class DeliveryDispatch1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ReportType"] != null)
                {
                    ReportTypeHiddenField.Value = Request.QueryString["ReportType"];
                    switch (ReportTypeHiddenField.Value)
                    {
                        case "DeliveryDispatch":
                            ReportTitleLabel.Text = Dictionary.Translate("Report.DeliveryDispatch");
                            break;
                        case "OpenMaterial":
                            ReportTitleLabel.Text = Dictionary.Translate("Reports.OpenMaterialReport");
                            break;
                        case "FutureInventory":
                            ReportTitleLabel.Text = Dictionary.Translate("Reports.FutureInventoryReport");
                            break;
                        default:
                            ReportTitleLabel.Text = Dictionary.Translate("Report.DeliveryDispatch");
                            break;
                    }
                }
                else
                {
                    ReportTypeHiddenField.Value = "DeliveryDispatch";
                    ReportTitleLabel.Text = Dictionary.Translate("Report.DeliveryDispatch");
                }

                ProductDDL.Items.Add(new ListItem("All products", "All products"));
                List<string> products = MLY.Bll.ReportsGenerator.GetOrganizationProducts(SessParam.OrganizationId, SessParam.IsClient);
                foreach (string s in products) ProductDDL.Items.Add(new ListItem(s, s));
                ProductDDL.SelectedValue = "All products";

                CreateReport("1", "1", "All products");
            }
            else 
            {
                CreateReport(DatesTypeDDL.SelectedValue, IntervalDDL.SelectedValue, ProductDDL.SelectedValue);
            }
        }

        
        protected void RefreshReport(object sender, EventArgs e)
        {
            CreateReport(DatesTypeDDL.SelectedValue, IntervalDDL.SelectedValue, ProductDDL.SelectedValue);
        }

        private void CreateReport(string DivisionDate, string Interval, string ProductName)
        {
            int ReportType = int.Parse(Interval);
            int DivDate = int.Parse(DivisionDate);
            double dInterval = double.Parse(Interval);
            bool byProduct = (GroupByDDL.SelectedValue == "1");

            if (byProduct)
            {
                ReportGrid.Columns[0].Visible = true;
                ReportGrid.Columns[0].VisibleIndex = 0;
                ReportGrid.Columns[1].Visible = false;
            }
            else
            {
                ReportGrid.Columns[0].Visible = false;
                ReportGrid.Columns[1].VisibleIndex = 0;
                ReportGrid.Columns[1].Visible = true;
            }

            for (int i = 2; i <= 22; i++)
            {
                ReportGrid.Columns[i].Visible = ColumnVisible(i, ReportType);
                ReportGrid.Columns[i].Caption = Header(ReportType, i);
                ReportGrid.Columns[i].Width = (ReportType >= 30) ? 80 : 37;
                if (ReportType < 7)
                {
                    ReportGrid.Columns[i].Width = 37;
                    ReportGrid.Columns[i].CellStyle.Font.Size = 9;
                }
                else if (ReportType < 30) 
                {
                    ReportGrid.Columns[i].Width = 90;
                    ReportGrid.Columns[i].CellStyle.Font.Size = 10;
                }
                else 
                {
                    ReportGrid.Columns[i].Width = 120;
                    ReportGrid.Columns[i].CellStyle.Font.Size = 10;
                }

            }

            //ReportDataSource.SelectParameters.Clear();
            //ReportDataSource.SelectParameters.Add("OrganizationId", SessParam.OrganizationId.ToString());
            //ReportDataSource.SelectParameters.Add("IsClient", SessParam.IsClient ? "1" : "0");
            //ReportDataSource.SelectParameters.Add("DivisionDate", DivisionDate);
            //ReportDataSource.SelectParameters.Add("Interval", Interval);
            switch (ReportTypeHiddenField.Value)
            { 
                case "DeliveryDispatch":
                    ReportGrid.DataSource = MLY.Bll.ReportsGenerator.DeliveryDispatchReport(SessParam.OrganizationId, SessParam.IsClient, DivDate, dInterval, ProductName, byProduct);
                    break;
                case "OpenMaterial":
                    ReportGrid.DataSource = MLY.Bll.ReportsGenerator.OpenMaterialReport(SessParam.OrganizationId, SessParam.IsClient, DivDate, dInterval, ProductName, byProduct);
                    break;
                case "FutureInventory":
                    ReportGrid.DataSource = MLY.Bll.ReportsGenerator.FutureInventory(SessParam.OrganizationId, SessParam.IsClient, DivDate, dInterval, ProductName, byProduct);
                    break;
                default:
                    ReportGrid.DataSource = MLY.Bll.ReportsGenerator.DeliveryDispatchReport(SessParam.OrganizationId, SessParam.IsClient, DivDate, dInterval, ProductName, byProduct);
                    break;
            }
            
            ReportGrid.DataBind();
        }

        protected void btnXlsExport_Click(object sender, EventArgs e)
        {
            Response.Clear();
            gridExport.WriteXlsToResponse();

        }
        protected void btnXlsxExport_Click(object sender, EventArgs e)
        {
            Response.Clear();
            gridExport.WriteXlsxToResponse();
        }
        protected void btnRtfExport_Click(object sender, EventArgs e)
        {
            Response.Clear();
            gridExport.WriteRtfToResponse();
        }
        protected void btnCsvExport_Click(object sender, EventArgs e)
        {
            gridExport.WriteCsvToResponse();
        }

        private string Header(int divtype, int ColumnNum)
        {
            string ActualHeader = "";
            DateTime Date = DateTime.Now.AddDays((ColumnNum-2) * divtype);
            if (divtype == 1)
            {
                ActualHeader = Date.Day.ToString() + "/" + Date.Month.ToString();
            }
            else
            {
                //System.Globalization.DateTimeFormatInfo inf = new System.Globalization.DateTimeFormatInfo();
                //ActualHeader = inf.GetAbbreviatedMonthName(Date.Month) + " " + Date.Year.ToString();
                ActualHeader = Date.Day.ToString() + "/" + Date.Month.ToString() + " - " +
                               Date.AddDays(divtype - 1).Day.ToString() + "/" + Date.AddDays(divtype - 1).Month.ToString();
            }

            return ActualHeader;
        }

        private bool ColumnVisible(int ColNum, int divtype)
        {
            if (divtype < 7) return true;
            if (divtype < 30 && ColNum <= 9) return true;
            if (divtype >= 30 && ColNum <= 7) return true;
            return false;
        }

        
    }
}
